package pl.koziolekweb.szkola.GGRysuj;

// GG-Rysuj/MyPanel.java
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyPanel extends JPanel
    implements MouseListener,MouseMotionListener,KeyListener
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int xOLD, yOLD;
    Graphics  pg;
    Color fore = Color.YELLOW;

      // implementacja interfejsu MouseListener
    public void mousePressed(MouseEvent e) {
        xOLD = e.getX();
        yOLD = e.getY();
        pg.setColor(fore);
    }
    public void mouseReleased(MouseEvent ignore) { }
    public void mouseClicked(MouseEvent  ignore) { }
    public void mouseEntered(MouseEvent  ignore) { }
    public void mouseExited(MouseEvent   ignore) { }

      // implementacja interfejsu MouseMotionListener
    public void mouseDragged(MouseEvent e)  {
        int x = e.getX(), y = e.getY();
        pg.drawLine(xOLD,yOLD,x,y);
        xOLD = x;
        yOLD = y;
    }
    public void mouseMoved(MouseEvent ignore)  { }

      // implementacja interfejsu KeyListener
    public void keyPressed(KeyEvent e)  {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_R: fore = Color.RED;    break;
            case KeyEvent.VK_G: fore = Color.GREEN;  break;
            case KeyEvent.VK_B: fore = Color.BLUE;   break;
            case KeyEvent.VK_Y: fore = Color.YELLOW; break;
        }
    }
    public void keyReleased(KeyEvent ignore) { }
    public void keyTyped(KeyEvent    ignore) { }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (pg != null) pg.dispose();
        pg = getGraphics();
        requestFocus();     // zeby panel sluchal klawiszy
    }
}
